package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategory1;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 一级分类controller层
 * @author 搭忆长安
 * @version 1.0
 * @date 2021年06月25日 20:06
 * @ClassName:ValidateActionJson
 * @Description: TODO
 */
@RestController
@RequestMapping(value = "/category1")
public class BaseCategory1Controller {
    @Autowired
    private BaseCategory1Service baseCategory1Service;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping(value = "findById/{id}")
    public Result<BaseCategory1> findById(@PathVariable(value = "id") Integer id){
        BaseCategory1 category1 = baseCategory1Service.getById(id);
        return Result.ok(category1);
    }
    /**
     * 查询所有的一级分类
     * @return
     */
    @GetMapping(value = "/findAll")
    public Result<List<BaseCategory1>> findAll(){
        List<BaseCategory1> baseCategory1s = baseCategory1Service.list(null);
        return Result.ok(baseCategory1s);
    }

    /**
     * 添加一级分类
     * @param baseCategory1
     * @return
     */
   @PostMapping(value = "/add")
    public Result save(@RequestBody BaseCategory1 baseCategory1){
        baseCategory1Service.save(baseCategory1);
        return Result.ok();
   }

    /**
     * 修改一级分类
     * @param baseCategory1
     * @return
     */
    @PutMapping(value = "/update")
    public Result update(@RequestBody BaseCategory1 baseCategory1){
        baseCategory1Service.updateById(baseCategory1);
        return Result.ok();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable(value = "id")Long id){
        baseCategory1Service.removeById(id);
        return Result.ok();
    }

    /**
     * 条件查询
     * @param baseCategory1
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<BaseCategory1>> search(@RequestBody BaseCategory1 baseCategory1){
        List<BaseCategory1> search = baseCategory1Service.search(baseCategory1);
        return Result.ok(search);
    }

    @GetMapping(value = "/getPage/{page}/{size}")
    public Result<List<BaseCategory1>> getPage(
                                               @PathVariable(value = "page")Integer page,
                                               @PathVariable(value = "size")Integer size){

        List<BaseCategory1> search = baseCategory1Service.pageSearch(page, size);
        return Result.ok(search);
    }

    /**
     * 分页条件查询
     * @param baseCategory1
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/searchByxx/{page}/{size}")
    public Result<List<BaseCategory1>> searchByxx(@RequestBody BaseCategory1 baseCategory1,
                                              @PathVariable(value = "page")Integer page,
                                              @PathVariable(value = "size")Integer size){

        List<BaseCategory1> search = baseCategory1Service.search(baseCategory1, page, size);
        return Result.ok(search);
    }
}
